﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace HappyLine
{


    public enum HappyLineState
    {
        ERROR,
        DELETED,
        LOADED,
        LOADING
    }
   
    public delegate void HappyLineHandler(object sender,HappyLineState hls);

    public partial class LineForm : Form
    {
      

        public event HappyLineHandler HappyLineStateChanged;
        string ImageUrl = string.Empty;
        string ImageFile = string.Empty;
        bool ErrorLoadFromWeb = false;

        public string Url
        {
            get { return ImageUrl; }
            set { ImageUrl = value; }
        }
        string name = string.Empty;

        public string LineName
        {
            get { return name; }
            set { name = value; }
        }

        public bool InitLine(string name, string url)
        {
            if (!LineLoadFromWeb(url))
                return false;

            this.ImageUrl = url;            
            this.ImageFile = ImageUrl.Substring(ImageUrl.LastIndexOf('/') + 1);
            this.Text = name;
            this.Visible = true;
          
            menuStrip1.Visible = true;
            this.ImageUrl = url;
            this.name = name;
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            this.Text = name;
            this.timer1.Enabled = true;
            this.happyLineToolStripMenuItem.Text = String.Format("Happy line '{0}' options", name);
            return true;
        }

        public void SetPlacement(int Left, int Top) 
        {
            this.Left = Left;
            this.Top = Top;
            this.FormBorderStyle = FormBorderStyle.None;
            menuStrip1.Visible = false;
        }
        private bool LineLoadFromWeb(string url)
        {
            try
            {
                pictureBox1.Load(url);
            }catch
            {
                return false;
            }
            return true;
        }
        private void LoadLineAsync(string source)
        {
            pictureBox1.CancelAsync();
            pictureBox1.LoadAsync(source);
            HappyLineStateChanged(this, HappyLineState.LOADING);
        }

        private bool SaveImage() 
        {
            try
            {
                pictureBox1.Image.Save(ImageUrl.Substring(ImageUrl.LastIndexOf('/') + 1));
            }
            catch 
            {
                return false;
            }
            return true;
        }

        public LineForm()
        {
            InitializeComponent();
        }

        private void changePossitionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetMenuVisible();
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            
           // button1.Visible = true;
        }

        public void SetMenuVisible()
        {
            menuStrip1.Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;
            
            menuStrip1.Visible = false;
            this.Refresh();
            //this.TopLevel = false;
          //  button1.Visible = false;
        }

        private void placeThisToolStripMenuItem_Click(object sender, EventArgs e)
        {
            menuStrip1.Visible = false;
            this.FormBorderStyle = FormBorderStyle.None;
            this.Refresh() ;
        }
        Color saved_pic1 = new Color();
        private void transperentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (transperentToolStripMenuItem.Checked)
            {
                this.BackColor = Color.Lime;
                this.TransparencyKey = Color.Lime;
                saved_pic1 = this.pictureBox1.BackColor;
                this.pictureBox1.BackColor = Color.Transparent;
                this.Refresh();
            }
            else 
            {
                this.BackColor = System.Drawing.SystemColors.ActiveBorder;
                this.TransparencyKey = Color.Empty;
                this.pictureBox1.BackColor = saved_pic1;
            }
        }

        private void LineForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            HappyLineStateChanged(this, HappyLineState.DELETED);
        }

        private void removeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            HappyLineStateChanged(this, HappyLineState.DELETED);
            Close();
        }

        private void LineForm_ResizeBegin(object sender, EventArgs e)
        {
            
        }

        private void LineForm_VisibleChanged(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.Visible = true;
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HappyLineStateChanged(this, HappyLineState.DELETED);
            Close();
        }

        private void colorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK) 
            {
                this.pictureBox1.BackColor = Color.Transparent;
                this.BackColor = colorDialog1.Color;
                this.TransparencyKey = Color.Empty;
            }
        }

        private void LineForm_Load(object sender, EventArgs e)
        {
            pictureBox1.WaitOnLoad = false;
            pictureBox1.LoadCompleted += new AsyncCompletedEventHandler(pictureBox1_LoadCompleted);
        }

        void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
                return;

            if (e.Error != null)
            {
                if (!ErrorLoadFromWeb)
                {
                    ErrorLoadFromWeb = true;
                    LoadLineAsync(ImageFile);
                    HappyLineStateChanged(this, HappyLineState.ERROR);
                }
                ErrorLoadFromWeb = false;
            }
            else
            {
                if(!ErrorLoadFromWeb)
                    SaveImage();
                HappyLineStateChanged(this, HappyLineState.LOADED);
            }
        }

        

        private void timer1_Tick(object sender, EventArgs e)
        {
            LoadLineAsync(ImageUrl);
        }
    }
}
